#pragma once
#include <memory>
#include <functional>
#include "fields.hpp"

namespace bitrpc {

//1.
    class BaseMessage
    {
    public:
        using ptr = std::shared_ptr<BaseMessage>;

        virtual ~BaseMessage() {}
        virtual void SetId(const std::string &rid) { _rid = rid; }
        virtual std::string GetId() { return _rid; }
        virtual void SetMType(MType type) { _type = type; }
        virtual MType GetMType() { return _type; }
        virtual std::string serialize() = 0;
        virtual bool unserialize(const std::string &msg) = 0;
        virtual bool check() = 0;

    private:
        MType _type;
        std::string _rid;
    };

//2.
   class BaseBuffer {
        public:
            using ptr = std::shared_ptr<BaseBuffer>;
            virtual size_t readableSize() = 0;//可读取 数据大小

            virtual int32_t peekInt32() = 0;//peek 窥视
            virtual void retrieveInt32() = 0;//re tri eve 移除
            virtual int32_t readInt32() = 0;//peek+retrieve 窥视+移除

            virtual std::string retrieveAsString(size_t len) = 0;//读取
    };

//3.
 class BaseProtocol {//协议
        public:
            using ptr = std::shared_ptr<BaseProtocol>;
            virtual bool canProcessed(const BaseBuffer::ptr &buf) = 0;
//检查 缓冲区 数据是否足够
            virtual bool onMessage(const BaseBuffer::ptr &buf, BaseMessage::ptr &msg) = 0;
//从 缓冲区中 解析出一条数据           
            virtual std::string serialize(const BaseMessage::ptr &msg) = 0;
//序列化
    };

    class BaseConnection {
        public:
            using ptr = std::shared_ptr<BaseConnection>;
            virtual void send(const BaseMessage::ptr &msg) = 0;
//通过连接 发送
            virtual void shutdown() = 0;
//关闭连接
            virtual bool connected() = 0;
//建立连接
    };

//4.
//类型别名
    using ConnectionCallback = std::function<void(const BaseConnection::ptr&)>;
    using CloseCallback = std::function<void(const BaseConnection::ptr&)>;
    using MessageCallback = std::function<void(const BaseConnection::ptr&, BaseMessage::ptr&)>;

class BaseServer {
        public:
            using ptr = std::shared_ptr<BaseServer>;
            virtual void setConnectionCallback(const ConnectionCallback& cb) {
                _cb_connection = cb;
                //！！！！！！！！实现和BaseConnection部分的解耦合
            }
//新建立连接时的 回调函数
            virtual void setCloseCallback(const CloseCallback& cb) {
                _cb_close = cb;
            }
            virtual void setMessageCallback(const MessageCallback& cb) {
                _cb_message = cb;
            }
//启动服务器
            virtual void start() = 0;
        protected:
            ConnectionCallback _cb_connection;
            CloseCallback _cb_close;
            MessageCallback _cb_message;
    };

//5.
class BaseClient {
        public:
            using ptr = std::shared_ptr<BaseClient>;
            virtual void setConnectionCallback(const ConnectionCallback& cb) {
                _cb_connection = cb;
            }
            virtual void setCloseCallback(const CloseCallback& cb) {
                _cb_close = cb;
            }
            virtual void setMessageCallback(const MessageCallback& cb) {
                _cb_message = cb;
            }//消息到来



            virtual void connect() = 0;
            virtual void shutdown() = 0;
            virtual bool send(const BaseMessage::ptr&) = 0;
            virtual BaseConnection::ptr connection() = 0;
            virtual bool connected() = 0;
        protected:
            ConnectionCallback _cb_connection;
            CloseCallback _cb_close;
            MessageCallback _cb_message;
    };
}